home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / GLE / TEXAS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  5.2 KB  |  229 lines

  1. /* 
  2.  * texas.c
  3.  * 
  4.  * FUNCTION:
  5.  * Draws a brand in the shape of Texas.  Both the handle, and the
  6.  * cross-section of the brand are in the shape of Texas.
  7.  *
  8.  * Note that the contours are specified in clockwise order. 
  9.  * Thus, enabling backfacing polygon removal will cause the front
  10.  * polygons to disappear.
  11.  *
  12.  * HISTORY:
  13.  * -- created by Linas Vepstas October 1991
  14.  * -- heavily modified to draw more texas shapes, Feb 1993, Linas
  15.  * -- converted to use GLUT -- December 1995, Linas
  16.  *
  17.  */
  18.  
  19. /* required include files */
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include <GL/glut.h>
  23. #include <GL/tube.h>
  24.  
  25. /* =========================================================== */
  26.  
  27. #define HNUM 4
  28. double brand_points[HNUM][3];
  29. float brand_colors [HNUM][3];
  30.  
  31. #define TSCALE  4.0
  32.  
  33. #define BPTS(x,y,z) {                \
  34.    brand_points[i][0] = TSCALE * (x);        \
  35.    brand_points[i][1] = TSCALE * (y);        \
  36.    brand_points[i][2] = TSCALE * (z);        \
  37.    i++;                        \
  38. }
  39.  
  40. #define BCOLS(r,g,b) {                \
  41.    brand_colors[i][0] = (r);             \
  42.    brand_colors[i][1] = (g);            \
  43.    brand_colors[i][2] = (b);            \
  44.    i++;                        \
  45. }
  46.  
  47. #define NUMPOINTS 18
  48. double tspine[NUMPOINTS][3];
  49. float tcolors [NUMPOINTS][3];
  50.  
  51. #define TPTS(x,y) {                \
  52.    tspine[i][0] = TSCALE * (x);        \
  53.    tspine[i][1] = TSCALE * (y);        \
  54.    tspine[i][2] = TSCALE * (0.0);        \
  55.    i++;                        \
  56. }
  57.  
  58. #define TCOLS(r,g,b) {                \
  59.    tcolors[i][0] = (r);                \
  60.    tcolors[i][1] = (g);                \
  61.    tcolors[i][2] = (b);                \
  62.    i++;                        \
  63. }
  64.  
  65. /* =========================================================== */
  66.  
  67. void init_spine (void)
  68. {
  69.    int i;
  70.    int ir, ig, ib;
  71.    float r, g, b;
  72.  
  73.    i=0;
  74.    TPTS (-1.5, 2.0);    /* panhandle */
  75.    TPTS (-0.75, 2.0);
  76.    TPTS (-0.75, 1.38);
  77.    TPTS (-0.5, 1.25);
  78.    TPTS (0.88, 1.12);
  79.    TPTS (1.0, 0.62);
  80.    TPTS (1.12, 0.1);
  81.    TPTS (0.5, -0.5);
  82.    TPTS (0.2, -1.12);    /* corpus */
  83.    TPTS (0.3, -1.5);    /* brownsville */
  84.    TPTS (-0.25, -1.45);
  85.    TPTS (-1.06, -0.3);
  86.    TPTS (-1.38, -0.3);
  87.    TPTS (-1.65, -0.6);
  88.    TPTS (-2.5, 0.5);   /* midland */
  89.    TPTS (-1.5, 0.5);
  90.    TPTS (-1.5, 2.0);    /* panhandle */
  91.    TPTS (-0.75, 2.0);
  92.  
  93.    ir = ig = ib = 0;
  94.    for (i=0; i<NUMPOINTS; i++) {
  95.       ir += 33; ig +=47; ib +=89;
  96.       ir %= 255; ig %= 255; ib %= 255;
  97.       r = ((float) ir) / 255.0;
  98.       g = ((float) ig) / 255.0;
  99.       b = ((float) ib) / 255.0;
  100.       
  101.       tcolors[i][0] = (r);
  102.       tcolors[i][1] = (g);
  103.       tcolors[i][2] = (b);
  104.    }
  105.  
  106.    i=0;
  107.    BPTS (0.0, 0.0, 0.1);
  108.    BPTS (0.0, 0.0, 0.0);
  109.    BPTS (0.0, 0.0, -5.0);
  110.    BPTS (0.0, 0.0, -5.1);
  111.  
  112.    i=0;
  113.    BCOLS (1.0, 0.3, 0.0);
  114.    BCOLS (1.0, 0.3, 0.0);
  115.    BCOLS (1.0, 0.3, 0.0);
  116.    BCOLS (1.0, 0.3, 0.0);
  117. }
  118.  
  119. /* =========================================================== */
  120.  
  121. #define SCALE 0.8
  122. #define BORDER(x,y) {                        \
  123.    double ax, ay, alen;                        \
  124.    texas_xsection[i][0] = SCALE * (x);                \
  125.    texas_xsection[i][1] = SCALE * (y);                \
  126.    if (i!=0) {                            \
  127.       ax = texas_xsection[i][0] - texas_xsection[i-1][0];        \
  128.       ay = texas_xsection[i][1] - texas_xsection[i-1][1];        \
  129.       alen = 1.0 / sqrt (ax*ax + ay*ay);            \
  130.       ax *= alen;   ay *= alen;                    \
  131.       texas_normal [i-1][0] = - ay;                \
  132.       texas_normal [i-1][1] = ax;                \
  133.    }                                \
  134.    i++;                                \
  135. }
  136.  
  137. #define NUM_TEXAS_PTS (17)
  138.  
  139. double texas_xsection [NUM_TEXAS_PTS][2];
  140. double texas_normal [NUM_TEXAS_PTS][2];
  141.  
  142. void init_xsection (void)
  143. {
  144.    int i;
  145.  
  146.    /* outline of extrusion */
  147.    i=0;
  148.    BORDER (-0.75, 2.0);
  149.    BORDER (-0.75, 1.38);
  150.    BORDER (-0.5, 1.25);
  151.    BORDER (0.88, 1.12);
  152.    BORDER (1.0, 0.62);
  153.    BORDER (1.12, 0.1);
  154.    BORDER (0.5, -0.5);
  155.    BORDER (0.2, -1.12);    /* corpus */
  156.    BORDER (0.3, -1.5);    /* brownsville */
  157.    BORDER (-0.25, -1.45);
  158.    BORDER (-1.06, -0.3);
  159.    BORDER (-1.38, -0.3);
  160.    BORDER (-1.65, -0.6);
  161.    BORDER (-2.5, 0.5);   /* midland */
  162.    BORDER (-1.5, 0.5);
  163.    BORDER (-1.5, 2.0);    /* panhandle */
  164.    BORDER (-0.75, 2.0);
  165. }
  166.  
  167.    
  168. /* =========================================================== */
  169.  
  170. void InitStuff (void)
  171. {
  172.    int style;
  173.  
  174.    /* configure the pipeline */
  175.    init_spine ();
  176.    init_xsection ();
  177.  
  178.    style = TUBE_JN_CAP;
  179.    style |= TUBE_CONTOUR_CLOSED;
  180.    style |= TUBE_NORM_FACET;
  181.    style |= TUBE_JN_ANGLE;
  182.    gleSetJoinStyle (style);
  183. }
  184.  
  185. /* =========================================================== */
  186.  
  187. extern float lastx;
  188. extern float lasty;
  189.  
  190. void DrawStuff (void) {
  191.  
  192. #ifdef IBM_GL_32
  193.    scale (1.8, 1.8, 1.8);
  194.  
  195.    lmcolor (material_mode);
  196.    (void) extrusion (NUM_TEXAS_PTS-1, texas_xsection, texas_normal, 
  197.                     NULL, NUMPOINTS, tspine, tcolors);
  198.  
  199.    (void) extrusion (NUM_TEXAS_PTS-1, texas_xsection, texas_normal, 
  200.                     NULL, HNUM, brand_points, brand_colors);
  201.  
  202.    lmcolor (LMC_COLOR);
  203. #endif
  204.  
  205. #define OPENGL_10
  206. #ifdef OPENGL_10
  207.    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  208.  
  209.    /* set up some matrices so that the object spins with the mouse */
  210.    glPushMatrix ();
  211.    glTranslatef (0.0, 0.0, -80.0);
  212.    glRotatef (lastx, 0.0, 1.0, 0.0);
  213.    glRotatef (lasty, 1.0, 0.0, 0.0);
  214.  
  215.    /* draw the brand and the handle */
  216.    gleExtrusion (NUM_TEXAS_PTS-1, texas_xsection, texas_normal, 
  217.                  NULL, NUMPOINTS, tspine, tcolors);
  218.  
  219.    gleExtrusion (NUM_TEXAS_PTS-1, texas_xsection, texas_normal, 
  220.                  NULL, HNUM, brand_points, brand_colors);
  221.  
  222.    glPopMatrix ();
  223.    glutSwapBuffers ();
  224. }
  225. #endif
  226.  
  227. /* ===================== END OF FILE ================== */
  228.  
  229.